package cn.shuangze.assetsms.view;


import cn.shuangze.assetsms.dao.PersonInfoDao;
import cn.shuangze.assetsms.dao.impl.PersonInfoDaoJDBCImpl;
import cn.shuangze.assetsms.entity.PersonInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class AddPersonInfo extends JDialog implements ActionListener {

    //定义DAO
    private PersonInfoDao personInfoDao;

    //定义所用的面板
    private JPanel westPanel = new JPanel();
    private JPanel eastPanel = new JPanel();
    private JPanel southPanel = new JPanel();
    private JPanel centerPanel = new JPanel();

    //框架的大小
    private Dimension viewSize = new Dimension(400, 400);
    private Font font = new Font("Dialog", 0, 12);

    //定义图形界面元素
    private JLabel[] jLabel = new JLabel[5];
    private JTextField[] jTextField = new JTextField[5];
    private JComboBox<String> jComboBox = new JComboBox<>(new String[]{"男", "女"});

    private JLabel nullLabel = new JLabel("  ");
    private JButton addBtn = new JButton("增加",new ImageIcon("images\\save.png"));
    private JButton clearBtn = new JButton("重置",new ImageIcon("images\\delete.png"));


    public AddPersonInfo() {

        personInfoDao = new PersonInfoDaoJDBCImpl();

        try {

            init();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "窗口初始化错误，错误原因：" + e.getMessage(),
                    "错误", JOptionPane.ERROR_MESSAGE);

            this.dispose();
        }
    }


    private void init() {
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());

        westPanel.setLayout(new GridLayout(5, 1, 0, 12));
        centerPanel.setLayout(new GridLayout(5, 1, 0, 12));

        this.setModal(true);
        this.setSize(viewSize);

        //设置标题
        this.setTitle("人员信息增加");
        this.setResizable(false);

        //设置运行时窗口的位置
        this.setLocationRelativeTo(null);


        for (int i = 0; i < jLabel.length; i++) {
            jLabel[i] = new JLabel();
            jLabel[i].setFont(font);
            westPanel.add(jLabel[i]);
        }

        jLabel[0].setText("   姓名   ");
        jLabel[1].setText("   性别   ");
        jLabel[2].setText("   部门   ");
        jLabel[3].setText("   职位   ");
        jLabel[4].setText("   其他   ");


        for (int i = 0; i < jTextField.length; i++) {
            jTextField[i] = new JTextField();
            jTextField[i].setColumns(15);
            if (i == 1) {
                jComboBox.setFont(font);
                centerPanel.add(jComboBox);
            } else {
                centerPanel.add(jTextField[i]);
            }
        }

        addBtn.setFont(font);
        clearBtn.setFont(font);
        southPanel.add(addBtn);
        southPanel.add(clearBtn);
        eastPanel.add(nullLabel);

        contentPane.add(nullLabel, BorderLayout.NORTH);
        contentPane.add(westPanel, BorderLayout.WEST);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(southPanel, BorderLayout.SOUTH);
        contentPane.add(eastPanel, BorderLayout.EAST);


        //添加事件
        addBtn.addActionListener(this);
        clearBtn.addActionListener(this);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == clearBtn) {
            for (JTextField j : jTextField) {
                j.setText(null);
            }

        } else if (obj == addBtn) {

            try {
                String name = jTextField[0].getText();
                String sex = (jComboBox.getSelectedIndex() == 0) ? "男" : "女";
                String dept = jTextField[2].getText();
                String job = jTextField[3].getText();
                String other = jTextField[4].getText();


                if (name.equals("")) {

                    JOptionPane.showMessageDialog(this, "姓名不能为空！",
                            "添加失败", JOptionPane.WARNING_MESSAGE);
                    return;

                } else if (dept.equals("")) {

                    JOptionPane.showMessageDialog(this, "部门不能为空！",
                            "添加失败", JOptionPane.WARNING_MESSAGE);
                    return;

                } else if (job.equals("")) {

                    JOptionPane.showMessageDialog(this, "职位不能为空！",
                            "添加失败", JOptionPane.WARNING_MESSAGE);
                    return;

                } else {
                    personInfoDao.add(new PersonInfo(name, sex, dept, job, other));

                    JOptionPane.showMessageDialog(this, "人员添加成功！",
                            "添加成功", JOptionPane.INFORMATION_MESSAGE);

                    for (JTextField j : jTextField) {
                        j.setText(null);
                    }
                }

            } catch (Exception ex) {

                JOptionPane.showMessageDialog(this, "数据库访问错误，错误原因：" + ex.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);

            }
        }
    }
}
